package vn.com.bookstore.activity;

import java.util.ArrayList;

import vn.com.bookstore.BookStoreApplication;
import vn.com.bookstore.ConnectActivity;
import vn.com.bookstore.ObjectFragment;
import vn.com.bookstore.R;
import vn.com.bookstore.adapter.BookViewAdapter;
import vn.com.bookstore.database.smng.BookMarkMng;
import vn.com.bookstore.database.sobject.BookMark;
import vn.com.bookstore.fragment.PageBookFragment;
import vn.com.bookstore.fragment.PageBookFragment.OnZoomListener;
import vn.com.bookstore.model.Book;
import vn.com.bookstore.model.Page;
import vn.com.bookstore.util.transformer.DepthPageTransformer;
import vn.com.bookstore.util.transformer.NonSwipeViewPager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

public class BookViewActivity extends ConnectActivity implements OnZoomListener{
	NonSwipeViewPager mPager;
	BookViewAdapter mAdapter;
	RelativeLayout mLayoutBtn;
	ImageButton mBtnLeft,mBtnRight;
	
	Book book;
	Page currentPage;
	int position=0;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_book_view);
		
		Bundle bundle = this.getIntent().getExtras();
		if(null != bundle){
			if(bundle.containsKey(Book.TAG)) book = (Book)bundle.getSerializable(Book.TAG);
			if(bundle.containsKey(Page.TAG)) currentPage = (Page)bundle.getSerializable(Page.TAG);
			if(bundle.containsKey("position")) position = bundle.getInt("position");
		}
		
		if(book == null || book.pages == null || book.pages.isEmpty()){
			finish();
			return;
		}
		
//		if(currentPage == null || currentPage.page == -1){
//			currentPage = book.pages.get(0);
//		}
		
		setTitle(book.pages.get(0).title);
		findViewById();
		
		mAdapter = new BookViewAdapter(this,getSupportFragmentManager(), book);
		mPager.setAdapter(mAdapter);
		mPager.setCurrentItem(position);
		
		
		
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				refreshUI();
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		boolean canScroll = true;
		if(book.isComics()){
			canScroll= false;
			mLayoutBtn.setVisibility(View.VISIBLE);
			if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
				mPager.setPageTransformer(true, new DepthPageTransformer());
			}
		}else{
			mLayoutBtn.setVisibility(View.GONE);
		}
		mPager.setCanScrollHorizontally(NonSwipeViewPager.SCROLL_LEFT, canScroll);
		mPager.setCanScrollHorizontally(NonSwipeViewPager.SCROLL_RIGHT, canScroll);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		BookMarkMng mng = new BookMarkMng(this);
		BookMark bookMark = new BookMark(-1, book.id+"", mPager.getCurrentItem()+"",((ObjectFragment)mAdapter.getItem(mPager.getCurrentItem())).getCurrentPosition()+"",System.currentTimeMillis()+"");
		ArrayList<BookMark> lstBookMark = mng.select(BookMarkMng.bookId, book.id+"");
		if(lstBookMark!=null && lstBookMark.size()>0){
			mng.edit(lstBookMark.get(0)._id, bookMark.getValues());
		}else{
			mng.insertNewTo(bookMark.getValues());
		}
	}
	
	@Override
	protected void findViewById(){
		mPager = (NonSwipeViewPager)findViewById(R.id.pager);
		mLayoutBtn=(RelativeLayout)findViewById(R.id.layout_btn);
		mBtnLeft = (ImageButton)findViewById(R.id.btn_left);
		mBtnRight = (ImageButton)findViewById(R.id.btn_right);
	}
	
	public void btnClicked(View v){
		((ObjectFragment)mAdapter.getListFragment().get(mPager.getCurrentItem())).btnClicked(v);
		switch (v.getId()) {
		case R.id.btn_left:
			if(mPager.getChildCount()>0 && mPager.getCurrentItem()>0){
				mPager.setCurrentItem(mPager.getCurrentItem()-1, true);
			}
			break;
		case R.id.btn_right:
			if(mPager.getChildCount()>0 && mPager.getCurrentItem()<(mPager.getChildCount()-1)){
				mPager.setCurrentItem(mPager.getCurrentItem()+1, true);
			}
			break;
		default:
			break;
		}
		refreshUI();
	}
	
	@Override
	public boolean onKeyDown(int keycode, KeyEvent e) {
		if(((ObjectFragment)mAdapter.getListFragment().get(mPager.getCurrentItem())).onKeyDown(keycode, e)){
			return true;
		}
		return super.onKeyDown(keycode, e);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		mAdapter.setData(book);
		
		int pageId = mApplication.getValue(BookStoreApplication.KEY_PAGE_ID, 0);
		for(int i=0;i<book.pages.size();i++){
			if(book.pages.get(i).id == pageId){
				mPager.setCurrentItem(i);
				((PageBookFragment)mAdapter.getListFragment().get(i)).scrollTo(mApplication.getValue(BookStoreApplication.KEY_LAST_Y, 0));
				((PageBookFragment)mAdapter.getListFragment().get(i)).refreshUI();
				book.bookMark = true;
				book.pages.get(i).bookMark = true;
				break;
			}
		}
	}

	@Override
	public void onZoomChange(float currentTextSize) {
		mApplication.setValue(BookStoreApplication.KEY_TEXT_SIZE, PageBookFragment.currentSize);
		for(Fragment itemFragment: mAdapter.getListFragment()){
			PageBookFragment page = (PageBookFragment)itemFragment;
			page.setTextCurrentSize(currentTextSize);
		}
	}
	
	@Override
	protected void refreshUI() {
		super.refreshUI();
		if(book.isComics()){
			mLayoutBtn.setVisibility(View.VISIBLE);
		}else{
			mLayoutBtn.setVisibility(View.GONE);
		}
//		if(mPager.getChildCount()<2 || !book.isComics()){
//			mLayoutBtn.setVisibility(View.GONE);
//		}else{
//			mLayoutBtn.setVisibility(View.VISIBLE);
//		}
//		if(mPager.getCurrentItem()==0){
//			mBtnLeft.setVisibility(View.INVISIBLE);
//		}else{
//			mBtnLeft.setVisibility(View.VISIBLE);
//		}
//		if(mPager.getCurrentItem()==mPager.getChildCount()-1){
//			mBtnRight.setVisibility(View.INVISIBLE);
//		}else{
//			mBtnRight.setVisibility(View.VISIBLE);
//		}
	}
}
